package com.lixingye.meowstar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lixingye.meowstar.common.AppResult;
import com.lixingye.meowstar.common.ResultCode;
import com.lixingye.meowstar.dao.MessageMapper;
import com.lixingye.meowstar.exception.ApplicationException;
import com.lixingye.meowstar.model.Message;
import com.lixingye.meowstar.model.User;
import com.lixingye.meowstar.service.IMessageService;
import com.lixingye.meowstar.service.IUserService;
import com.lixingye.meowstar.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 猫大仙
 * @since 2025-01-14
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {
    @Autowired
    private IUserService userService;
    @Autowired
    private MessageMapper messageMapper;
    private Integer getUnreadCou(Long rUI){
        return messageMapper.selectList(new LambdaQueryWrapper<Message>()
                .eq(Message::getState,0)
                .eq(Message::getDeleteState,0)
                .eq(Message::getReceiveUserId,rUI)).size();
    }
    @Override
    public void create(Message message) {
        User user = userService.getById(message.getReceiveUserId());
        //校验
        if(message == null || message.getPostUserId() == null
                || message.getReceiveUserId() == null || StringUtil.isEmpty(message.getContent())
                || user==null || user.getDeleteState()==1){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //设置默认值
        message.setState((byte) 0);
        message.setDeleteState((byte) 0);
        //设置创建和更新时间
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        int insert = messageMapper.insert(message);
        if(insert!=1){
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        if(receiveUserId==null || receiveUserId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Integer unreadCou = getUnreadCou(receiveUserId);
        if(unreadCou==null){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        return unreadCou;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        if(receiveUserId == null || receiveUserId <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        return messages;
    }

    @Override
    public void updateStateById(Long id, Byte State) {
        if(id == null || id <= 0 || State<0 || State>2){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message updateMessage = new Message();
        updateMessage.setId(id);
        updateMessage.setState(State);
        Date date = new Date();
        updateMessage.setUpdateTime(date);
        int i = messageMapper.updateById(updateMessage);
        if(i!=1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void reply(Long repliedId, Message message) {
        if (repliedId == null || repliedId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message exitMessage = messageMapper.selectById(repliedId);
        if(exitMessage == null || exitMessage.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        //更新 已回复
        updateStateById(repliedId, (byte) 2);
        create(message);
    }
}
